package com.repair.user.facade;

import com.repair.api.user.constant.UserOperateType;
import com.repair.api.user.request.*;
import com.repair.api.user.request.condition.UserCondition;
import com.repair.api.user.request.condition.UserIdQueryCondition;
import com.repair.api.user.request.condition.UserPhoneQueryCondition;
import com.repair.api.user.request.param.UserQueryParam;
import com.repair.api.user.request.param.UserUpdateStatusParam;
import com.repair.api.user.response.UserOperateResponse;
import com.repair.api.user.response.UserQueryResponse;
import com.repair.api.user.response.data.UserInfo;
import com.repair.api.user.service.UserFacadeService;
import com.repair.base.exception.SystemErrorCode;
import com.repair.base.exception.SystemException;
import com.repair.base.response.NoResultResponse;
import com.repair.base.response.PageResponse;
import com.repair.rpc.config.Facade;
import com.repair.user.pojo.entity.User;
import com.repair.user.service.UserOperateStreamService;
import com.repair.user.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author chunyu
 * @since 2024/9/21,15:30
 */
@DubboService
public class UserFacadeServiceImpl implements UserFacadeService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserOperateStreamService userOperateStreamService;

    @Override
    @Facade
    public UserQueryResponse<UserInfo> query(UserQueryRequest userQueryRequest) {
        UserCondition userCondition = userQueryRequest.getUserCondition();
        User user = null;
        if (userCondition instanceof UserPhoneQueryCondition) {
            user = userService.selectByTelephone(((UserPhoneQueryCondition) userCondition).getTelephone());
        } else if (userCondition instanceof UserIdQueryCondition) {
            user = userService.selectById(((UserIdQueryCondition) userCondition).getId());
        }
        UserQueryResponse<UserInfo> response = new UserQueryResponse<>();
        if (user == null) {
            response.setSuccess(false);
            return response;
        }
        UserInfo userInfo = new UserInfo();

        BeanUtils.copyProperties(user, userInfo);
        // 9-26 bug 没设置值 所以查询只会是null
        response.setData(userInfo);
        response.setSuccess(true);
        return response;
    }

    @Override
    @Facade
    public UserOperateResponse register(UserRegisterRequest userRegisterRequest) {
       return userService.register(userRegisterRequest);
    }

    @Override
    @Facade
    public UserOperateResponse loginStream(UserLoginStreamRequest<UserInfo> userLoginStreamRequest) {
        UserInfo userInfo = userLoginStreamRequest.getData();
        User user = new User();
        BeanUtils.copyProperties(userInfo, user);
        long l = userOperateStreamService.insertStream(user, UserOperateType.LOGIN);
        UserOperateResponse userOperateResponse = new UserOperateResponse();
        if (l == 0) {
            throw new SystemException(SystemErrorCode.INSERT_FAILED);
        }
        userOperateResponse.setSuccess(true);
        return userOperateResponse;
    }

    @Override
    @Facade
    public PageResponse<UserInfo> getAllUsers(UserQueryPageRequest<UserQueryParam> queryParam) {
        return userService.getAllUsers(queryParam);
    }

    @Override
    @Facade
    public NoResultResponse updateUserStatus(UserUpdateStatusRequest<UserUpdateStatusParam> userUpdateRequest) {
        return userService.updateUserInfo(userUpdateRequest);
    }

    @Override
    @Facade
    public UserQueryResponse<Boolean> blockUser(UserBlockRequest request) {
        return userService.blockUser(request);
    }


}
